package com.lxmes.business.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lxmes.base.domain.JcIteminfo;
import com.lxmes.base.domain.NcSupplier;
import com.lxmes.base.service.INcSupplierService;
import com.lxmes.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.business.domain.CcPurchaseInfo;
import com.lxmes.business.service.ICcPurchaseInfoService;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 采购单Controller
 *
 * @author 张志煜
 * @date 2022-11-27
 */
@RestController
@RequestMapping("/business/purchaseInfo")
public class CcPurchaseInfoController extends BaseController
{
    @Autowired
    private ICcPurchaseInfoService ccPurchaseInfoService;

    @Autowired
    private INcSupplierService ncSupplierService;


    /**
     * 查询采购单列表
     */
    @PreAuthorize("@ss.hasPermi('business:purchaseInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(CcPurchaseInfo ccPurchaseInfo)
    {
        startPage();
        List<CcPurchaseInfo> list = ccPurchaseInfoService.selectCcPurchaseInfoList(ccPurchaseInfo);
        return getDataTable(list);
    }
    /**
     * 查询采购单列表
     */
    @GetMapping("/selectList")
    public TableDataInfo selectList(CcPurchaseInfo ccPurchaseInfo)
    {
        startPage();
        List<CcPurchaseInfo> list = ccPurchaseInfoService.selectCcPurchaseInfoSelectList(ccPurchaseInfo);
        return getDataTable(list);
    }

    @GetMapping("/applyList")
    public TableDataInfo applyList(CcPurchaseInfo ccPurchaseInfo)
    {
        List<CcPurchaseInfo> list = ccPurchaseInfoService.selectCcPurchaseInfoApplyList(ccPurchaseInfo);
        return getDataTable(list);
    }


    /**
     * 导出采购单列表
     */
    @PreAuthorize("@ss.hasPermi('business:purchaseInfo:export')")
    @Log(title = "采购单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CcPurchaseInfo ccPurchaseInfo)
    {
        List<CcPurchaseInfo> list = ccPurchaseInfoService.selectCcPurchaseInfoList(ccPurchaseInfo);
        ExcelUtil<CcPurchaseInfo> util = new ExcelUtil<CcPurchaseInfo>(CcPurchaseInfo.class);
        util.exportExcel(response, list, "采购单数据");
    }

    /**
     * 获取采购单详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:purchaseInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ccPurchaseInfoService.selectCcPurchaseInfoById(id));
    }

    /**
     * 新增采购单
     */
    @PreAuthorize("@ss.hasPermi('business:purchaseInfo:add')")
    @Log(title = "采购单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CcPurchaseInfo ccPurchaseInfo)
    {
        return toAjax(ccPurchaseInfoService.insertCcPurchaseInfo(ccPurchaseInfo));
    }
    /**
     * 添加采购物料明细
     */
    @GetMapping("/addCaigoItemInfo/{ids}")
    public AjaxResult addCaigoItemInfo(@PathVariable Long[] ids)
    {
        List<CcPurchaseInfo> list = ccPurchaseInfoService.addCaigoItemInfo(ids);
        return AjaxResult.success(list);
    }
    /**
     * 修改采购单
     */
    @PreAuthorize("@ss.hasPermi('business:purchaseInfo:edit')")
    @Log(title = "采购单", businessType = BusinessType.UPDATE)
    @PostMapping("/updatePurchaseInfo")
    public AjaxResult edit(@RequestBody CcPurchaseInfo ccPurchaseInfo)
    {
        return toAjax(ccPurchaseInfoService.updateCcPurchaseInfo(ccPurchaseInfo));
    }
    /**
     * 审批采购单
     */
    @PreAuthorize("@ss.hasPermi('business:purchaseInfo:shenpi')")
    @Log(title = "采购单审批", businessType = BusinessType.UPDATE)
    @PostMapping("/shenpi")
    public AjaxResult shenpi(@RequestBody CcPurchaseInfo ccPurchaseInfo)
    {
        return toAjax(ccPurchaseInfoService.updateShenPi(ccPurchaseInfo));
    }
    /**
     * 采购单生成合同
     */
    @PreAuthorize("@ss.hasPermi('business:purchaseInfo:htsc')")
    @Log(title = "采购单生成合同", businessType = BusinessType.UPDATE)
    @PostMapping("/htsc")
    public AjaxResult htsc(@RequestBody CcPurchaseInfo ccPurchaseInfo)
    {
        return toAjax(ccPurchaseInfoService.updateShenHtSc(ccPurchaseInfo));
    }
    @Log(title = "采购单申请", businessType = BusinessType.UPDATE)
    @PostMapping("/apply")
    public AjaxResult apply(@RequestBody CcPurchaseInfo ccPurchaseInfo)
    {
        return toAjax(ccPurchaseInfoService.apply(ccPurchaseInfo));
    }



    /**
     * 删除采购单
     */
    @PreAuthorize("@ss.hasPermi('business:purchaseInfo:remove')")
    @Log(title = "采购单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ccPurchaseInfoService.deleteCcPurchaseInfoByIds(ids));
    }


    @Log(title = "导入采购计划", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport,String yhCompany,String yhTime,String yhType,String supplierCode,String cgName,Long deptId) throws Exception
    {
        ExcelUtil<JcIteminfo> util = new ExcelUtil<>(JcIteminfo.class);
        List<JcIteminfo> infoList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        CcPurchaseInfo ccPurchaseInfo = new CcPurchaseInfo();
        ccPurchaseInfo.setYhCompany(yhCompany);
        ccPurchaseInfo.setYhTime(DateUtils.dateTime("yyyy-MM-dd",yhTime));
        if(supplierCode!=null){
            NcSupplier ncSupplier = new NcSupplier();
            ncSupplier.setSupplierCode(supplierCode);
            List<NcSupplier> ncSuppliers = ncSupplierService.selectNcSupplierList(ncSupplier);
            ccPurchaseInfo.setSupplyId(ncSuppliers.get(0).getId());
            ccPurchaseInfo.setSupplyName(ncSuppliers.get(0).getSupplierName());
        }
        ccPurchaseInfo.setYhType(yhType);
        ccPurchaseInfo.setCgName(cgName);
        ccPurchaseInfo.setDeptId(deptId);
        String message = ccPurchaseInfoService.imporIteminfo(infoList, updateSupport, operName,ccPurchaseInfo);
        return AjaxResult.success(message);
    }

}
